from rest_framework.generics import ListCreateAPIView
from mg_admin.utils import PageNumPagination
from mg_admin.serializers.user import UserSerializer,UserAddSerializer
from userapp.models import Users



class UserAPIView(ListCreateAPIView):
    '''
    list:查询所有用户信息
    create: 新增用户信息
    '''
    pagination_class = PageNumPagination
    def get_serializer_class(self):
        if self.request.method == 'GET':
            return UserSerializer
        else:
            return UserAddSerializer

    def get_queryset(self):
        # 获取请求参数
        q = self.request.query_params.get('q')
        # 判断是否为空
        if q =='' or q is None:
            return Users.objects.order_by('-id')
        else:
            return Users.objects.filter(username=q).order_by('-id')

